package cn.edu.csust.coursecard.utils.storage;

import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

public class FileUtils {
    public static String createFileName(MultipartFile file) {
        if (file.isEmpty() || StringUtils.isEmpty(file.getOriginalFilename())) {
            throw new BaseException(CodeEnum.PARAM_ERROR.getCode(), "图片错误");
        }
        String originalFilename = file.getOriginalFilename();
        String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        String fileNamePrefix = "" + System.currentTimeMillis() + uuid.substring(0, uuid.indexOf("-"));
        return fileNamePrefix + fileNameSuffix;
    }

    public static String createFileName() {
        String fileNameSuffix = ".jpg";
        String uuid = UUID.randomUUID().toString();
        String fileNamePrefix = "" + System.currentTimeMillis() + uuid.substring(0, uuid.indexOf("-"));
        return fileNamePrefix + fileNameSuffix;
    }
}
